// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// y [author] rhyme_lph
// o [email]  rhymelph@gmail.com
// K [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/entities/account_entity.dart';
import 'package:finance_app/src/http_service/http_account_service.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/page/dashboard/bank/bank_account_page.dart';

class BankAccountProvider extends BaseProvider<List<AccountEntity>> {
  AccountEntity currentSelect;

  @override
  Future get dataFuture => HttpAccountService.accountList();

  void onAddBankAccount() async {
    final result =
        await Routes.navigateTo(RouterGenProviders.addBankAccountPage);
    if (result != null) {
      if (data == null) {
        data = [result];
      } else {
        data.add(result);
      }
      currentSelect = result;
      update();
      obtainContext?.call((context) {
        Navigator.of(context).pop(result);
      });
    }
  }

  void onChangeBankAccount(AccountEntity value) {
    obtainContext?.call((context) {
      Navigator.of(context).pop(value);
    });
  }
}
